<?php
/**
 * @category   Application_Extensions
 * @package    Talktangle Plugin 
 * @copyright  Idragon Co., Ltd.
 * @license
 */
class Talktangle_Widget_TalktangleController extends Engine_Content_Widget_Abstract {
		
	public function indexAction() {
	   $request = Zend_Controller_Front::getInstance()->getRequest();
       $talktangle_id = $request->getParam('id',0);
              
       if(empty($talktangle_id)){
            return $this->setNoRender();
       }
	   if (!Engine_Api::_()->core()->hasSubject('user')) {
            return $this->setNoRender();
	   }
	   // Get subject
	   $this->view->subject = $viewer = $subject = Engine_Api::_()->core()->getSubject('user');
	
       $coreApi =  Engine_Api::_()->talktangle();
        
       $talktangle = Engine_Api::_()->getItem('talktangle',$talktangle_id); 
       if(!$talktangle){
            $this->view->error = true;
            $this->view->message = $this->view->translate('Can not found this tangel, please select other tangle.');
       }
       
       
       $this->view->untangle = $untangle = false;
       if($talktangle->status == 1){
            $this->view->untangle = $untangle = true;
       }

       $this->view->form = $form = new Talktangle_Form_Talktangle_Untangle();    
        
       $talktangle = Engine_Api::_()->getItem('talktangle', $talktangle_id);
        
       $form->populate(array('title'=>$talktangle->content));
            
       $this->view->preview = $talktangle->preview;
     
        // Check post
        if(!$this->getRequest()->isPost()){
            return;
        }
        $this->view->isValidF = true;
        // Check form validate
        if(!$form->isValid($this->getRequest()->getPost())){
            $this->view->isValidF = false;
            return;
        }
        
        $values = $form->getValues(); 
        // Process
        $db = Engine_Db_Table::getDefaultAdapter();
        $db->beginTransaction();                
        try{    
            $talktangle->untangle_date = date('Y-m-d H:i:s');
            $talktangle->winner_id = $viewer->getIdentity();
            $talktangle->status = 1;
            $talktangle->save();
            
            $viewer->points = $viewer->points + 5;
            $viewer->untangle = $viewer->untangle + 1;
            $viewer->save();
            $db->commit();   
        }
        catch(Exception $e)
        {
            $db->rollback();
            throw $e;
        }
        
        $this->view->url = $url =  Zend_Controller_Front::getInstance()->getRouter()
            ->assemble(array('controller'=>'index','action'=>'index'), 'talktangle_extended', true);
        header("Location: $url");           
	}
    
    public function paypalreturnAction(){
    
        $state = $this->_getParam('state');
        $order_id = $this->_getParam('order_id');
        $tangle_id = $this->_getParam('tangle_id');
        $type = $this->_getParam('type');
        
        $order = Engine_Api::_()->getItem('payment_order',$order_id);
        $tangle = Engine_Api::_()->getItem($type,$tangle_id);             
        if($state == 'return'){
            //change order status
            $order->state = 'completed';
            //tangle status
            $tangle->status = 2; //payment complete
            
            return $this->_helper->redirector->gotoRoute(array('controller'=>'index','action'=>'index','type'=>'createtalk'), 'talktangle_extended', true);
        }
        elseif($state == 'cancel'){
            //tangle status
            $tangle->status = 3;  //payment pending          
            //return create form
            if($type=='talktangle'){
                return $this->_helper->redirector->gotoRoute(array('controller'=>'index','action'=>'index','type'=>'createtalk','tangle'=>$tangle_id), 'talktangle_extended', true);
            }
            
        }
  }    
}